;;; mini-init.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 gotpl
;;
;; Author: gotpl <631707329@qq.com>
;; Maintainer: gotpl <631707329@qq.com>
;; Created: 2024-02-09 13:16:18
;; Modified: 2024-08-13 00:26:56
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Description
;;
;;; Code:

(setq plugin-path-list '(
                        ;;  "elpaca/repos/use-package"
                         "elpaca/repos/markdown-mode"
                         "elpaca/repos/popon"
                         "elpaca/repos/acm-terminal"
                         "elpaca/repos/lsp-bridge"
                         "elpaca/repos/yasnippet"
                         "elpaca/repos/yasnippet-snippets"
                         "elpaca/repos/pyvenv"
                        ;;  "elpaca/repos/pyvenv"
                        ;;  "elpaca/repos/compat"
                        ;;  "elpaca/repos/vertico"
                         "elpaca/repos/orderless"
                        ;;  "elpaca/repos/vertico"

                        ;;  "elpaca/repos/consult")
                         ))

(dolist (plugin-path plugin-path-list)
  (add-to-list 'load-path (expand-file-name plugin-path user-emacs-directory)))


(setq emacs-python-path "")
(let ((emacs-python-env (getenv "EMACS_PYTHON_PATH"))
      (pyvenv-work-dir (getenv "WORKON_HOME")))
  (if (not pyvenv-work-dir)
      (setenv "WORKON_HOME" "~/miniconda3/envs"))
  (if (not emacs-python-env)
    (setq emacs-python-path (expand-file-name "~/miniconda3/envs/emacs/bin/python3.11"))
    (setq emacs-python-path emacs-python-env)))

(defun autoinsert-yas-expand()
  "Replace text in yasnippet template."
  (yas-expand-snippet (remove-yas-header-comment (buffer-string)) (point-min) (point-max)))
(require 'yasnippet-snippets)
(require 'autoinsert)
(let ((template-dir (expand-file-name "snippets/file-templates/" user-emacs-directory)))
    (auto-insert-mode 1)

    (setq auto-insert-directory template-dir)
    (setq auto-insert-query nil) ;; If you don't want to be prompted before insertion
    (setq auto-insert-alist
          (append '(
                    (emacs-lisp-mode . ["emacs-lisp-mode/__package" autoinsert-yas-expand])
                    ;; (sh-mode . ["sh-mode/__" autoinsert-yas-expand])
                    )
                  auto-insert-alist))
    ;; (define-auto-insert "\\.py$" ["python-mode/__" autoinsert-yas-expand])
    ;; (define-auto-insert "\\.el$" ["emacs-lisp-mode/__package" autoinsert-yas-expand])
    ;; C/C++
    (define-auto-insert "\\.py$" ["python-mode/__" autoinsert-yas-expand])
    (define-auto-insert "\\.[Hh]$" ["c-mode/__h" autoinsert-yas-expand])
    (define-auto-insert "\\.sh$" ["sh-mode/__" autoinsert-yas-expand])
    (define-auto-insert "\\.hpp$" ["c-mode/__h" autoinsert-yas-expand])
    (define-auto-insert "\\.c$" ["c-mode/__c" autoinsert-yas-expand])
    (define-auto-insert "\\.cpp$" ["c-mode/__c" autoinsert-yas-expand])
    (define-auto-insert "main.cpp" ["c++-mode/__main.cpp" autoinsert-yas-expand])
    )
(auto-insert-mode 1)
(require 'lsp-bridge)
(setq lsp-bridge-enable-inlay-hint t
        lsp-bridge-enable-hover-diagnostic t)
(setq lsp-bridge-python-command emacs-python-path
        acm-quick-access-use-number-select t
        )
(add-to-list 'lsp-bridge-formatting-indent-alist '(c++-ts-mode . c-basic-offset))
(defun gcc-is-executable ()
  (executable-find "gcc"))

(defun linux/get-arch ()
  (replace-regexp-in-string "\n" "" (shell-command-to-string "uname -m")))

(defun enable-lsp-bridge()
  (when-let* ((gcc-executable-p (gcc-is-executable))
              (arch (linux/get-arch))
              (lsp-dir (expand-file-name (format "lsp-bridge-server/gcc/%s" arch)user-emacs-directory)))
    (setq lsp-bridge-user-langserver-dir lsp-dir
                lsp-bridge-user-multiserver-dir lsp-dir))
  (global-lsp-bridge-mode))
(enable-lsp-bridge)

;; (setq emacs-python-path "")
;; (let ((emacs-python-env (getenv "EMACS_PYTHON_PATH"))
;;       (pyvenv-work-dir (getenv "WORKON_HOME")))
;;   (if (not pyvenv-work-dir)
;;       (setenv "WORKON_HOME" "~/miniconda3/envs"))
;;   (if (not emacs-python-env)
;;     (setq emacs-python-path (expand-file-name "~/miniconda3/envs/emacs/bin/python3.11"))
;;     (setq emacs-python-path emacs-python-env)))
;; (setq lsp-bridge-python-command emacs-python-path)

;;   (let ((work-on-home (getenv "WORKON_HOME")))
;;     (unless work-on-home
;;       (setq work-on-home "~/miniconda3/envs"))
;;     (setenv "WORKON_HOME" (expand-file-name work-on-home)))
  
;;   ;; (add-hook 'python-mode-hook (lambda () (setq-local lsp-bridge-get-single-lang-server-by-project 'local/lsp-bridge-get-single-lang-server-by-project)))

;;   (add-hook 'pyvenv-post-activate-hooks
;;             (lambda ()
;;               (lsp-bridge-restart-process)))
;; (require 'pyvenv)
;; (setq lsp-bridge-enable-log t)
;; (global-lsp-bridge-mode)
;; (defvar elpaca-installer-version 0.6)
;; (defvar elpaca-directory (expand-file-name "elpaca/" user-emacs-directory))
;; (defvar elpaca-builds-directory (expand-file-name "builds/" elpaca-directory))
;; (defvar elpaca-repos-directory (expand-file-name "repos/" elpaca-directory))
;; (defvar elpaca-order '(elpaca :repo "https://github.com/progfolio/elpaca.git"
;;                               :ref nil
;;                               :files (:defaults "elpaca-test.el" (:exclude "extensions"))
;;                               :build (:not elpaca--activate-package)))
;; (let* ((repo  (expand-file-name "elpaca/" elpaca-repos-directory))
;;        (build (expand-file-name "elpaca/" elpaca-builds-directory))
;;        (order (cdr elpaca-order))
;;        (default-directory repo))
;;   (add-to-list 'load-path (if (file-exists-p build) build repo))
;;   (unless (file-exists-p repo)
;;     (make-directory repo t)
;;     (when (< emacs-major-version 28) (require 'subr-x))
;;     (condition-case-unless-debug err
;;         (if-let ((buffer (pop-to-buffer-same-window "*elpaca-bootstrap*"))
;;                  ((zerop (call-process "git" nil buffer t "clone"
;;                                        (plist-get order :repo) repo)))
;;                  ((zerop (call-process "git" nil buffer t "checkout"
;;                                        (or (plist-get order :ref) "--"))))
;;                  (emacs (concat invocation-directory invocation-name))
;;                  ((zerop (call-process emacs nil buffer nil "-Q" "-L" "." "--batch"
;;                                        "--eval" "(byte-recompile-directory \".\" 0 'force)")))
;;                  ((require 'elpaca))
;;                  ((elpaca-generate-autoloads "elpaca" repo)))
;;             (progn (message "%s" (buffer-string)) (kill-buffer buffer))
;;           (error "%s" (with-current-buffer buffer (buffer-string))))
;;       ((error) (warn "%s" err) (delete-directory repo 'recursive))))
;;   (unless (require 'elpaca-autoloads nil t)
;;     (require 'elpaca)
;;     (elpaca-generate-autoloads "elpaca" repo)
;;     (load "./elpaca-autoloads")))
;; (add-hook 'after-init-hook #'elpaca-process-queues)
;; (elpaca `(,@elpaca-order))

;; (setq package-enable-at-startup nil)
;; (elpaca elpaca-use-package
;;   ;; Enable use-package :ensure support for Elpaca.
;;   (elpaca-use-package-mode))
;; (elpaca-wait)

;; (use-package evil-nerd-commenter
;;   :ensure t
;;   ;; :commands evil-
;;   :config
;;   (evilnc-default-hotkeys)
;;   ;; :commands evilnc-comment-or-uncomment-lines
;;   ;; :hook (prog-mode . (lambda ()
;;   ;;                      (require 'evil-nerd-commenter)))
;;   )
;; (require 'lsp-bridge)
;; (require 'recentf)
;; (recentf-mode)
;; (setq lsp-bridge-python-command "/home/gotpl/miniconda3/envs/emacs/bin/python")
;; (global-lsp-bridge-mode)
;; (require 'pyvenv)
;; (require 'consult)
;; (consult-customize consult-ripgrep consult-git-grep consult-grep
;;                        consult-bookmark
;;                        consult-recent-file
;;                        consult-buffer
;;                        ;; :preview-key nil ;; 禁止预览
;;                        )
;; (require 'vertico)
;; (vertico-mode)
;; (require 'orderless)
;; (setq completion-styles '(orderless))
;; (setenv "WORKON_HOME" "~/miniconda3/envs")

;; (defun change_venv()
;;   (interactive)
;;   (pyvenv-workon "torch210")
;;   (lsp-bridge-restart-process))
;; (global-set-key (kbd "C-x C-r") 'recentf)
;; (global-set-key (kbd "C-x C-R") 'consult-recent-file)

(provide 'mini-init)
;;; mini-init.el ends here
